package com.fairytask.ui.base
{
	import com.fairytask.engine.define.UIDefine;
	import com.fairytask.managers.ViewManage;
	import com.fairytask.ui.mainUnit.ProfileUI;
	import com.fairytask.ui.mainUnit.RecastUI;
	import com.fairytask.ui.mainUnit.RefineUI;
	import com.fairytask.ui.mainUnit.UniverseUI;
	
	import flash.display.Sprite;

	/**
	 * UI显示部件 
	 * @author hanjy
	 * 
	 */	
	public class UnitUI extends BaseUI
	{
		private var _uiSpace	:Sprite = new Sprite();
		//是否绑定
		protected var isBind		:Boolean	=	false;
		//重置标志,true重置，false不重置
		private var _needReset	:Boolean = true;
		//当前嵌套在的FrameUI名字
		public var curUIName		:String	=	"";
		
		public var currentUI		:BaseUI;
		public var soildUI			:Boolean	=	false;
		
		public function UnitUI()
		{
			super();
			if(null!=windowBG)
			{
				windowBG.removeDrag();
			}
		}
		
		/**
		 * 显示在 
		 * @param pUI 要显示的UI
		 * @param pX	界面X位置
		 * @param pY	界面Y位置
		 * 
		 */
		private function addShowUI(pUnitUI:BaseUI,pX:int=0,pY:int=0):void
		{
			pUnitUI.x = pX;
			pUnitUI.y = pY;
			//非指定UI
			if(!soildUI)
			{
				currentUI	=	pUnitUI;
			}
			_uiSpace.addChild(pUnitUI);
			
			if(null!=currentUI&&currentUI.uiName&&UIDefine[currentUI.uiName])
			{
				//通知服务器打开
				////NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_UI,UIDefine[currentUI.uiName]);
			}
		}
		
		/**
		 * ui显示刷新 
		 * @param uiArr
		 * 
		 */
		public function showUI(uiArr:Array,skin:Array):void
		{
			if(null == uiArr)
			{
				return;
			}
			if(!this.contains(_uiSpace))
			{
				addChild(_uiSpace);
			}
			//添加皮肤
			if(null != skin)
			{
				if(skin.length > 1)
				{
					skin[0].x = skin[1];
					skin[0].y = skin[2];
				}
				_uiSpace.addChild(skin[0]);
			}
			//遍历显示模块，逐一添加
			for(var i:int=0;i<uiArr.length;i++)
			{
				var tmpUI	:BaseUI = getUnitUI(uiArr[i][0]);
				if(uiArr[i].length > 1)
				{
					addShowUI(tmpUI,uiArr[i][1],uiArr[i][2]);
				}
				else
				{
					addShowUI(tmpUI);
				}
			}
			_needReset = true;
		}
		
		/**
		 * 清除UI层 
		 * 
		 */
		public function clearUI():void
		{
			while(_uiSpace.numChildren>0)
			{
				_uiSpace.removeChildAt(0);
			}
		}
		
		/**
		 * 获取所需的unitUI 
		 * @param UIid
		 * @return 
		 * 
		 */
		private function getUnitUI(id:int):BaseUI
		{
			var tmpUI	:BaseUI	= ViewManage.retrieveUIByID(id);
			switch(id)
			{
				case UIDefine.RoleListUI:
				case UIDefine.PetsListUI:
					if(_needReset)
					{
						tmpUI.reset();
						_needReset = false;
					}
					break;
				case UIDefine.ProfileUI:
					if(_needReset)
					{
						tmpUI.reset();
						_needReset = false;
					}
					ProfileUI.getInstance().curUIName = curUIName;
					break;
				case UIDefine.PetEquipUI:
					//宠物为空时候
					break;
				case UIDefine.SkillUI:
				case UIDefine.LineupUI:
				case UIDefine.PetsFuseUI:
				case UIDefine.PetsSwallowUI:
				case UIDefine.PackageUI:
					tmpUI.reset();
					break;
				case UIDefine.UniverseUI:
					UniverseUI.getInstance().UIShow();
					break;
				case UIDefine.RecastUI:
					RecastUI.getInstance().clear();
					break;
				case UIDefine.RefineUI:
					RefineUI.getInstance().clear();
					break;
				case UIDefine.SkillProfileUI:
				case UIDefine.ChartUI:
				case UIDefine.PetsProfileUI:
					break;
			}
			return tmpUI;
		}
	}
}